package com.dcbeida.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dcbeida.R;
import com.dcbeida.bean.Ckxq;
import com.dcbeida.bean.CkxqHeadBean;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 差异详情
 */
public class CyxqAdapter extends BaseAdapter {

    private ArrayList<Ckxq.ListBean> mArrayList = new ArrayList<>();
    private Context mContext;
    private HashMap<Integer, CkxqHeadBean> tagHashMap = new HashMap<>();

    public CyxqAdapter(Context context) {
        super();
        this.mContext = context;
    }

    public HashMap<Integer, CkxqHeadBean> getTagHashMap() {
        return tagHashMap;
    }


    @Override
    public int getCount() {
        if (mArrayList == null) {
            return 0;
        } else {
            return this.mArrayList.size();
        }
    }

    @Override
    public Object getItem(int position) {
        if (mArrayList == null) {
            return null;
        } else {
            return this.mArrayList.get(position);
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }



    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.cyxq_item_new, null, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Ckxq.ListBean bean = mArrayList.get(position);
        holder.tvPh.setText(bean.getBatchnumber());
        holder.tvBh.setText(bean.getDrugcode());
        holder.tvYxq.setText(mContext.getString(R.string.yxq) + bean.getExpiredate());


        if (tagHashMap.containsKey(position)) {//head 显示逻辑
            holder.lin_item_head.setVisibility(View.VISIBLE);
            holder.lin_item_tjs.setVisibility(View.VISIBLE);
            CkxqHeadBean ckBean = tagHashMap.get(position);
            if (ckBean != null) {
                holder.tv_yp_name.setText(ckBean.getDrugName());
                holder.tv_pds.setText(String.valueOf(ckBean.getInventory()));
                holder.tv_kcs.setText(String.valueOf(ckBean.getStock()));


                if (ckBean.getOverage() == 0) {
                    holder.tv_pys.setText("0");
                } else {
                    holder.tv_pys.setText("+" + ckBean.getOverage());

                }

                if (ckBean.getShortage() == 0) {
                    holder.tv_pks.setText("0");
                } else {
                    holder.tv_pks.setText("-" + ckBean.getShortage());
                }


            }

        } else {
            holder.lin_item_head.setVisibility(View.GONE);
            holder.lin_item_tjs.setVisibility(View.GONE);
        }

        int nextPosition = position + 1;
        if (tagHashMap.containsKey(nextPosition) || nextPosition >= mArrayList.size()) {//底部线高的判断
            holder.lin13.setVisibility(View.VISIBLE);
            holder.lin5.setVisibility(View.GONE);
        } else {
            holder.lin13.setVisibility(View.GONE);
            holder.lin5.setVisibility(View.VISIBLE);
        }

        if (0 > Integer.parseInt(bean.getDiff())) {
            holder.tvStatus.setText(mContext.getString(R.string.pk));
            holder.tvBoxNum.setText(bean.getDiff());
            holder.tvBox.setTextColor(mContext.getResources().getColor(R.color.c_0C87EA));
            holder.tvBoxNum.setTextColor(mContext.getResources().getColor(R.color.c_0C87EA));
        } else if (0 < Integer.parseInt(bean.getDiff())) {
            holder.tvStatus.setText(mContext.getString(R.string.py));
            holder.tvBoxNum.setText(bean.getDiff());
            holder.tvBox.setTextColor(mContext.getResources().getColor(R.color.c_F64545));
            holder.tvBoxNum.setTextColor(mContext.getResources().getColor(R.color.c_F64545));
        } else {
            holder.tvStatus.setText(mContext.getString(R.string.wyk));
            holder.tvBoxNum.setText(bean.getStock());
            holder.tvBox.setTextColor(mContext.getResources().getColor(R.color.c_333333));
            holder.tvBoxNum.setTextColor(mContext.getResources().getColor(R.color.c_333333));
        }

        return convertView;
    }


    public ArrayList<Ckxq.ListBean> getmArrayList() {
        return mArrayList;
    }

    public void setmArrayList(ArrayList<Ckxq.ListBean> mArrayList) {
        if (mArrayList.size() >= 0) {
            this.mArrayList.addAll(mArrayList);
        }
        notifyDataSetChanged();
    }

    public void clearList() {
        this.mArrayList.clear();
        this.tagHashMap.clear();
    }


    static class ViewHolder {


        @Bind(R.id.tv_ph)
        TextView tvPh;
        @Bind(R.id.tv_yxq)
        TextView tvYxq;
        @Bind(R.id.tv_bh)
        TextView tvBh;
        @Bind(R.id.tv_status)
        TextView tvStatus;
        @Bind(R.id.tv_box_num)
        TextView tvBoxNum;
        @Bind(R.id.tv_box)
        TextView tvBox;

        //新增
        @Bind(R.id.lin_item_head)
        LinearLayout lin_item_head;
        @Bind(R.id.lin_item_tjs)
        LinearLayout lin_item_tjs;
        @Bind(R.id.tv_yp_name)
        TextView tv_yp_name;
        @Bind(R.id.tv_pds)
        TextView tv_pds;
        @Bind(R.id.tv_kcs)
        TextView tv_kcs;
        @Bind(R.id.tv_pys)
        TextView tv_pys;
        @Bind(R.id.tv_pks)
        TextView tv_pks;
        @Bind(R.id.lin5)
        LinearLayout lin5;
        @Bind(R.id.lin13)
        LinearLayout lin13;


        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
